package com.hangyutech.strelitzia.modules.controller;

import com.github.pagehelper.PageInfo;
import com.hangyutech.strelitzia.modules.entity.Members;
import com.hangyutech.strelitzia.modules.service.MembersService;
import com.hangyutech.strelitzia.utils.RedisUtil;
import com.hangyutech.strelitzia.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * 用户表(Members)表控制层
 *
 * @author makejava
 * @since 2020-12-02 20:49:05
 */
@RestController
@RequestMapping("members")
@CrossOrigin
@Api(description = "用户列表")
public class MembersController {
    /**
     * 服务对象
     */
    @Resource
    private MembersService membersService;

    @Autowired
    private HttpServletRequest request;

    @Resource
    private RedisUtil redisUtil;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("/{id}")
    public HttpResult selectOne(
            @ApiParam(name = "id", value = "id", required = true)
            @PathVariable Integer id) {


        return HttpResult.ok()
                .data(this.membersService.queryById(id))
                .message("查询列表成功");
    }


    @ApiOperation(value = "增加新记录")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public HttpResult insert(
            @RequestBody Members members) {


        Date date = new Date(System.currentTimeMillis());
        members.setCreatedAt(date);
        Members data = membersService.insert(members);
        return Objects.isNull(data) ? HttpResult.error() : HttpResult.ok();
    }


    @GetMapping()
    @ApiOperation(value = "分页查看列表", notes = "分页查看列表")
    public HttpResult search(
            @RequestParam("pageIndex") int pageIndex,
            @RequestParam("pageSize") int pageSize) {


        PageInfo<Members> rowList = membersService.queryAllByPage(pageIndex, pageSize, "");

//        return HttpResult.ok();
//        List<Pictures> rowList = picturesService.queryAllByLimit(pageIndex,pageSize);
        return HttpResult.ok()
                .data(rowList)
                .message("查询列表成功");
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public HttpResult delete(
            @ApiParam(name = "id", value = "用户id", required = true)
            @PathVariable Integer id) {

        membersService.deleteById(id);
        return HttpResult.ok();
    }

}